package com.talkweb.ncfw.web.action;

import java.util.List;

import com.talkweb.ncfw.component.rights.entity.Role;
import com.talkweb.ncfw.component.rights.entity.User;
import com.talkweb.ncfw.component.rights.service.IUserManager;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.utils.StringUtils;
import com.talkweb.ncfw.web.struts2.ExtGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;

/**
 * <p>文件名称: UserAction.java</p>
 * <p>文件描述: 用户管理</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-9-26</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期: 2012/1/6
 *    修 改 人:Wuqingming
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Liuyizhi
 */
public class UserAction extends ExtGridBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 733011056753532957L;

	private IUserManager userManager;
	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IUserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * 
	 * @author：Liuyizhi 	        
	 * @date：2011-9-26
	 * @Description：分页查询用户列表
	 * @return
	 * @throws ViewBaseException
	 */
	public String listUser() throws ViewBaseException {
		Page<User> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap();
		try {
			page = this.userManager.findUser(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Liuyizhi 	        
	 * @date：2011-10-2
	 * @Description：添加用户
	 * @return
	 * @throws ViewBaseException
	 */
	public String addUser() throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String roleids = this.getParameter("roleids");
		if (StringUtils.isNotEmpty(roleids)) {
			String[] roleidArray = roleids.split(",");
			long userroleId = 0;
			for (String roleid : roleidArray) {
				userroleId += Long.parseLong(roleid);
			}
			user.setUserroleId(userroleId);
		}
		try {
			returnCode = this.userManager.addUser(user);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		flag = (returnCode == 1);
		msg = flag ? "用户添加成功" : "用户添加失败";
		
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Liuyizhi 	        
	 * @date：2011-9-26
	 * @Description：修改用户
	 * @return
	 * @throws ViewBaseException
	 */
	public String updateUser() throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String roleids = this.getParameter("roleids");
		if (StringUtils.isNotEmpty(roleids)) {
			String[] roleidArray = roleids.split(",");
			long userroleId = 0;
			for (String roleid : roleidArray) {
				userroleId += Long.parseLong(roleid);
			}
			user.setUserroleId(userroleId);
		}
		try {
			returnCode = this.userManager.updateUser(user);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		flag = (returnCode == 1);
		msg = flag ? "用户信息修改成功" : "用户信息修改失败";
		
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Liuyizhi 	        
	 * @date：2011-9-26
	 * @Description：删除用户
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteUser() throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String userIds = this.getParameter("userIds");
		String[] userIdArray = null;
		if (!StringUtils.isEmpty(userIds)) {
			userIdArray = userIds.split(",");
		}
		try {
			returnCode = this.userManager.deleteUserByUserId(userIdArray);
			flag = (returnCode == 1);
			msg = flag ? "用户删除成功" : "用户删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}

	/**
	 * 
	 * @author：Liuyizhi 	        
	 * @date：2011-9-26
	 * @Description：根据用户id获取用户信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String getUserByUserId() throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		String userId = condition.getString("userId");
		User user = null;
		try {
			user = this.userManager.getUserByUserId(userId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), user);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-9
	 * @Description：列出某角色下的用户信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String listUserInRoles () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		String roleIds = condition.getString("roleIds");
		condition.remove("roleIds");
		Page<User> page = this.getPage();
		String[] roleIdArray = StringUtils.isEmpty(roleIds) ? null : roleIds.split(",");
		try {
			page = this.userManager.findUserInRoles(page, roleIdArray, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-9
	 * @Description：列出不在某角色下的用户信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String listUserOutRoles () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		String roleIds = condition.getString("roleIds");
		condition.remove("roleIds");
		Page<User> page = this.getPage();
		String[] roleIdArray = StringUtils.isEmpty(roleIds) ? null : roleIds.split(",");
		try {
			page = this.userManager.findUserOutRoles(page, roleIdArray, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2012-1-6
	 * @Description：查询某用户拥有的角色
	 * @return
	 * @throws ViewBaseException
	 */
	public String findRoleByUser () throws ViewBaseException {
		String userId = this.getParameter("userId");
		List<Role> roleList = null;
		try {
			roleList = this.userManager.findRolesByUserid(userId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJsonArray(this.getResponse(), roleList);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2012-1-6
	 * @Description：用户启用
	 * @return
	 * @throws ViewBaseException
	 */
	public String enableUser() throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String userIds = this.getParameter("userIds");
		String[] userIdArray = null;
		if (!StringUtils.isEmpty(userIds)) {
			userIdArray = userIds.split(",");
		}
		try {
			returnCode = this.userManager.enableUser(userIdArray);
			flag = (returnCode == 1);
			msg = flag ? "用户启用成功" : "用户启用失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2012-1-6
	 * @Description：用户禁用
	 * @return
	 * @throws ViewBaseException
	 */
	public String disableUser() throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String userIds = this.getParameter("userIds");
		String[] userIdArray = null;
		if (!StringUtils.isEmpty(userIds)) {
			userIdArray = userIds.split(",");
		}
		try {
			returnCode = this.userManager.disableUser(userIdArray);
			flag = (returnCode == 1);
			msg = flag ? "用户禁用成功" : "用户禁用失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2012-1-7
	 * @Description：修改用户密码
	 * @return
	 * @throws ViewBaseException
	 */
	public String updateUserPwd () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String userid = this.getParameter("userid");
		String newpwd = this.getParameter("newpwd");
		try {
			returnCode = this.userManager.updatePassword(userid, newpwd);
			flag = (returnCode == 1);
			msg = flag ? "密码修改成功" : "密码修改失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}

}
